Attribute VB_Name = "modTD"
'---------------------------------------------------------------------------------------
'Module : modTD
'Date   : 03/07/2005
'Author : Joseph Weir
'Purpose: Copy of frmShip routines for TD transactions.
'
' 2005-04-28, JFW, Vantreight, corrections and testing.
' 2006-12-18, JFW, Child shipment IRS number saved to Client DB.
' 2007-05-15, JFW, TD MPS with FXRS 840.  New boolean TDChild for logic flow.
'
'---------------------------------------------------------------------------------------
'

Option Explicit

Public FormTD As frmTD
Public FormTDAdd As frmTDAdd

Public g_str542 As String
Public g_blnTD As Boolean                          'Set to True when TD is selected
Public g_blnTDChild As Boolean                     'Set to True when TD is selected
Public g_blnTDConfirm As Boolean                   'Set to True when confirming an TD shipment
Public g_strTDOpenShip As String                   'Current Open Ship Index
Public g_strTDMasterTrkNbr As String
Public g_dteTDDate As Date

'---------------------------------------------------------------------------------------
' Procedure : BuildTDCreateTrans
' DateTime  : 10/26/2004
' Purpose   : Builds an TD MAWB Transaction
'---------------------------------------------------------------------------------------
'
Public Function BuildTDCreateTrans() As String
    Dim strTrans As String
    
    On Error GoTo EH
    g_strErrorLocation = "modMain:BuildCloseTrans"
    
    strTrans = ""
    
    TransactionBuilder strTrans, 0, "020"                   'Trans type
    TransactionBuilder strTrans, 1, "TD MAWB CREATE"       'TransID
    ' 10,"128652841"
    ' TransactionBuilder strTrans, 20, g_str20                'Bill-To Acct#
    If FormTDAdd.f24 <> Date Then
        TransactionBuilder strTrans, 24, Format(FormTDAdd.f24, "YYYYMMDD")               'Ship date
        g_str24 = Format(FormTDAdd.f24, "YYYYMMDD")
    End If
    If FormTDAdd.f25 <> "" Then
        TransactionBuilder strTrans, 25, FormTDAdd.f25       'Reference
    End If
    TransactionBuilder strTrans, 50, FormTDAdd.tdReturn("f50")            'Recip Country
    If FormTDAdd.f66 <> "" Then
        TransactionBuilder strTrans, 66, FormTDAdd.f66
    End If
    If FormTDAdd.f67 <> "" Then
        TransactionBuilder strTrans, 67, FormTDAdd.f67
    End If
    TransactionBuilder strTrans, 68, FormTDAdd.f68                'Currency Type
    TransactionBuilder strTrans, 70, Left(FormTDAdd.f70, 1)               'Duty/Tax Pay Type
    TransactionBuilder strTrans, 71, FormTDAdd.f71                'Duty/Tax Acct#
    TransactionBuilder strTrans, 72, Left(FormTDAdd.f72, 1)               'Terms of Sale
    TransactionBuilder strTrans, 74, FormTDAdd.tdReturn("f50")                'Country of Ultimate Dest
    TransactionBuilder strTrans, 75, FormTDAdd.f75                'Weight type
    
    TransactionBuilder strTrans, 79, "Description of contents"
    TransactionBuilder strTrans, 80, "CA"
    

    ' TransactionBuilder strTrans, 117, g_str117              'Shipper country
    ' 2007-05-30, JFW, Why should the Ship screen affect CI/CCI
    ' If frmShip.optCommodities.Value Then
    If FormTDAdd.f113.Value = 1 Then
        TransactionBuilder strTrans, 113, g_str113              'CI Flag, this is CCI when on master
        If g_str538 <> "" Then
            TransactionBuilder strTrans, 538, g_str538                                  'CI Printer UNC
        End If
    End If
    
    ' IOR
    TransactionBuilder strTrans, 169, FormTDAdd.tdIOR("f50")
    TransactionBuilder strTrans, 170, FormTDAdd.tdIOR("f12")
    TransactionBuilder strTrans, 171, FormTDAdd.tdIOR("f11")
    TransactionBuilder strTrans, 172, FormTDAdd.tdIOR("f13")
    TransactionBuilder strTrans, 173, FormTDAdd.tdIOR("f14")
    TransactionBuilder strTrans, 174, FormTDAdd.tdIOR("f15")
    TransactionBuilder strTrans, 175, FormTDAdd.tdIOR("f16")
    TransactionBuilder strTrans, 176, FormTDAdd.tdIOR("f17")
    TransactionBuilder strTrans, 178, FormTDAdd.tdIOR("f18")
    TransactionBuilder strTrans, 180, FormTDAdd.tdIOR("CustID")
    
    TransactionBuilder strTrans, 498, frmSetup.f498         'Meter #
    TransactionBuilder strTrans, 541, "YNNNNNNNN"           'Open Ship Flag
    
    g_str542 = Format(Now, "ddHHnnss")                      'Open ship index
    TransactionBuilder strTrans, 542, g_str542              'Open ship index
    
    TransactionBuilder strTrans, 1090, g_str68              'Currency
    If g_str1139 <> "" Then
        TransactionBuilder strTrans, 1139, g_str1139        'Sender IRS
    End If
    
                                    ' BSO no-no, 1174,"Y"
                                '    TransactionBuilder strTrans, 1179, ""
                                '    TransactionBuilder strTrans, 1180, "Broker Company"
                                '    TransactionBuilder strTrans, 1181, "Broker Address Line 1"
                                '    TransactionBuilder strTrans, 1182, "Broker Address Line 2"
                                '    TransactionBuilder strTrans, 1183, "New York"
                                '    TransactionBuilder strTrans, 1184, "NY"
                                '    TransactionBuilder strTrans, 1185, "10018"
                                '    TransactionBuilder strTrans, 1186, "US"
                                    ' TransactionBuilder strTrans, 1187, "BSO001"
    
    TransactionBuilder strTrans, 1274, "82"                 'Service Type
    
    TransactionBuilder strTrans, 1297, g_str1297            'Customer Inv #
    
    ' TransactionBuilder strTrans, 1355, g_str1355            'IOR Facility Code
    ' TransactionBuilder strTrans, 1349, g_str1349            'AES Filing Status
    
    TransactionBuilder strTrans, 1358, g_str1358            'ITN/FTSR
    ' Trying to get something in FSMS export permit
    ' TransactionBuilder strTrans, 1399, "12345"
    TransactionBuilder strTrans, 1483, FormTDAdd.f1483       ' SCAC
    TransactionBuilder strTrans, 1484, IIf(FormTDAdd.f1484, "Y", "N")    ' FedEx linehaul
    If FormTDAdd.f1484 Then
        TransactionBuilder strTrans, 1498, Left(FormTDAdd.f1498, 1)   ' Payment type
        TransactionBuilder strTrans, 1500, FormTDAdd.f1500    ' Acct #
        ' Else
        ' TransactionBuilder strTrans, 1502, "N"
    End If
    
    ' TD return
    TransactionBuilder strTrans, 1485, FormTDAdd.tdReturn("f12")
    TransactionBuilder strTrans, 1486, FormTDAdd.tdReturn("f11")
    TransactionBuilder strTrans, 1487, FormTDAdd.tdReturn("f13")
    TransactionBuilder strTrans, 1488, FormTDAdd.tdReturn("f14")
    TransactionBuilder strTrans, 1489, FormTDAdd.tdReturn("f15")
    TransactionBuilder strTrans, 1490, FormTDAdd.tdReturn("f16")
    TransactionBuilder strTrans, 1491, FormTDAdd.tdReturn("f17")
    TransactionBuilder strTrans, 1492, FormTDAdd.tdReturn("f18")
    
    If FormTDAdd.f1502Y Then
        ' Tower group broker
        TransactionBuilder strTrans, 1502, "Y"
        TransactionBuilder strTrans, 1546, Left(FormTDAdd.f1546, 1)   ' Payment type
        TransactionBuilder strTrans, 1503, FormTDAdd.f1503    ' Acct #
        ' Else
        ' TransactionBuilder strTrans, 1502, "N"
    End If
    
    TransactionBuilder strTrans, 1666, FormTDAdd.tdReturn("f15")
    
    TransactionBuilder strTrans, 99, ""                     'End of Transaction
    
    Debug.Print strTrans
    
    BuildTDCreateTrans = strTrans
    
    Exit Function
EH:
    MsgBox Err.Description, vbCritical, g_strErrorLocation
End Function
'---------------------------------------------------------------------------------------
' Procedure : BuildTDShipTrans
' DateTime  : 10/26/2004
' Purpose   : Builds an TD CRN Add Transaction
'---------------------------------------------------------------------------------------
'
Public Function BuildTDShipTrans(ByVal strOrderNum As String, Optional PieceNumber As Integer = 1, Optional MasterTrackingNbr As String = "") As String
    Dim strTrans As String
    Dim strQuery As String
    Dim rsCommodity As New ADODB.Recordset
    Dim i As Integer
    
    On Error GoTo EH
    g_strErrorLocation = "modFXRSTransactions:BuildTDShipTrans"
    
    strQuery = "SELECT * FROM IntlCommodities WHERE f1 = '" & strOrderNum & "'"
    rsCommodity.Open strQuery, g_connFSMSClient, adOpenStatic, adLockOptimistic
    
    'Build the common fields
    strTrans = BuildCommonFlds
    
    TransactionBuilder strTrans, 26, Trim(Str(CLng(Val(g_str26))))       'Carriage Value
    TransactionBuilder strTrans, 57, g_str57                'DIM H
    TransactionBuilder strTrans, 58, g_str58                'DIM W
    TransactionBuilder strTrans, 59, g_str59                'DIM L
    TransactionBuilder strTrans, 1116, g_str1116            'DIM Units
    
    'Build the Intl MPS fields
    TransactionBuilder strTrans, 1358, g_str1358            'ITN/FTSR
    TransactionBuilder strTrans, 413, g_str413              'NAFTA Flag
    TransactionBuilder strTrans, 74, g_str74                'Country of Ultimate Dest
    TransactionBuilder strTrans, 75, g_str75                'Weight type
    TransactionBuilder strTrans, 72, "1"                    'Terms of Sale
    
    'Loop through commodities
    i = 1
    g_str119 = "0"
    
    ' 2006-10-02, JFW, TDPiece style 2nd piece and later commodities have no value
    If CBool(g_clsReg.GetRegistryValue("TDPieceFirstPieceValue", 1)) _
    And PieceNumber > 1 Then
    
        TransactionBuilder strTrans, "79", "Package " & PieceNumber & " of " & Val(g_str116) _
            & ", master tracking number " & MasterTrackingNbr
        TransactionBuilder strTrans, "82", "1"
        TransactionBuilder strTrans, "414", "EA"
        TransactionBuilder strTrans, "1030", "0"
    
    Else
        ' 2006-10-02, JFW, First piece, or old style FastShip
        Do While Not rsCommodity.EOF
            TransactionBuilder strTrans, "77-" & Trim(Str(i)), RemoveSpecial(rsCommodity("g_str77")) & ""           'Terms of Sale
            TransactionBuilder strTrans, "79-" & Trim(Str(i)), rsCommodity("g_str79") & ""                          'Description
            TransactionBuilder strTrans, "80-" & Trim(Str(i)), rsCommodity("g_str80") & ""                          'Country of Manu
            TransactionBuilder strTrans, "81-" & Trim(Str(i)), rsCommodity("g_str81") & ""                          'Harmonized Code
            TransactionBuilder strTrans, "82-" & Trim(Str(i)), rsCommodity("g_str82") & ""                          'Quantity
            TransactionBuilder strTrans, "83-" & Trim(Str(i)), rsCommodity("g_str83") & ""                          'Export license #
            TransactionBuilder strTrans, "84-" & Trim(Str(i)), Format(rsCommodity("g_str84"), "YYYYMMDD") & ""      'Export license Exp Date
            TransactionBuilder strTrans, "414-" & Trim(Str(i)), rsCommodity("g_str414") & ""                        'Unit of Measure
            TransactionBuilder strTrans, "1030-" & Trim(Str(i)), RemoveSpecial(ImpliedDecimal(rsCommodity("g_str1030"), 6)) & ""       'Commodity Unit Value
            'Add the Customs values of the individual commodities for a total
            g_str119 = Str(Val(g_str119) + Val(rsCommodity("g_str119"))) & ""
            i = i + 1
            rsCommodity.MoveNext
        Loop
    End If
    
    TransactionBuilder strTrans, 119, RemoveSpecial(ImpliedDecimal(g_str119))   'Total Customs value of shipment
    TransactionBuilder strTrans, 541, "NNNYNNNNN"                               'Open Ship Flags
    TransactionBuilder strTrans, 542, g_strTDOpenShip
    
    TransactionBuilder strTrans, 99, ""                             'End of Transaction
    
    BuildTDShipTrans = strTrans

Release:
    If rsCommodity.State = adStateOpen Then
        rsCommodity.Close
    End If
    Set rsCommodity = Nothing
    
    Exit Function
EH:
    MsgBox Err.Description, vbCritical, g_strErrorLocation
    Resume Release
    
End Function
'---------------------------------------------------------------------------------------
' Procedure : BuildTDConfirmTrans
' DateTime  : 10/26/2004
' Purpose   : Builds an TD Confirm Transaction
'---------------------------------------------------------------------------------------
'
Public Function BuildTDConfirmTrans() As String
    Dim strTrans As String
    
    On Error GoTo EH
    g_strErrorLocation = "modMain:BuildTDConfirmTrans"
    
    strTrans = ""
    With FormTD
        TransactionBuilder strTrans, 0, "020"                   'Trans type
        TransactionBuilder strTrans, 1, "TD MAWB CONFRM"       'TransID
        ' TransactionBuilder strTrans, 50, .grdTD.Columns(3)     'Recip Country
        TransactionBuilder strTrans, 498, frmSetup.f498         'Meter #
        TransactionBuilder strTrans, 541, "NNNNNYNNY"           'Open Ship Flag
        TransactionBuilder strTrans, 542, g_strTDOpenShip              'Open ship index
    End With
    
    TransactionBuilder strTrans, 99, ""                 'End of Transaction
    
    BuildTDConfirmTrans = strTrans
    
    Exit Function
EH:
    MsgBox Err.Description, vbCritical, g_strErrorLocation
End Function


'---------------------------------------------------------------------------------------
' Procedure : BuildCRNDeleteTrans
' DateTime  : 11/1/2004
' Purpose   : Deletes a trk#
'---------------------------------------------------------------------------------------
'
Public Function BuildTDCDeleteTrans(ByVal strTrackNum As String, ByVal strOpenShip As String) As String
    Dim strTrans As String
    
    On Error GoTo EH
    g_strErrorLocation = "modFXRSTransactions:BuildTDCDeleteTrans"
    
    'Save the trk# to global var
    g_str29 = strTrackNum
    
    'Set IPD flag to True
    g_blnTD = True
    
    strTrans = ""
    TransactionBuilder strTrans, 0, "020"                   'Transaction type
    TransactionBuilder strTrans, 1, "TD TDC DELETE"       'Transaction ID
    TransactionBuilder strTrans, 29, g_str29                'Tracking #
    TransactionBuilder strTrans, 498, g_str498              'Meter #
    TransactionBuilder strTrans, 541, "NNNNNNYNN"           'Open Ship Flags
    TransactionBuilder strTrans, 542, strOpenShip
    TransactionBuilder strTrans, 99, ""                     'End of Transaction
    
    BuildTDCDeleteTrans = strTrans
    
    Exit Function
EH:
     MsgBox Err.Description, vbCritical, g_strErrorLocation

End Function


'---------------------------------------------------------------------------------------
' Procedure : BuildTDDeleteTrans
' DateTime  : 10/26/2004
' Purpose   : Builds an TD Delete Shipment Transaction
'---------------------------------------------------------------------------------------
'
Public Function BuildTDDeleteTrans() As String
    Dim strTrans As String
    
    On Error GoTo EH
    g_strErrorLocation = "modMain:BuildTDDeleteTrans"
    
    strTrans = ""
    With FormTD
        
        TransactionBuilder strTrans, 0, "020"                                   'Trans type
        TransactionBuilder strTrans, 1, "TD MAWB DELETE"   'TransID
        TransactionBuilder strTrans, 29, g_strTDMasterTrkNbr                    'MAWB Trk#
        ' TransactionBuilder strTrans, 50, .grdTD.Columns(3)     'Recip Country
        TransactionBuilder strTrans, 498, frmSetup.f498         'Meter #
        TransactionBuilder strTrans, 541, "NNNNNNNYN"           'Open Ship Flag
        TransactionBuilder strTrans, 542, g_strTDOpenShip     'Open ship index
    
    End With
    
    TransactionBuilder strTrans, 99, ""                 'End of Transaction
    
    BuildTDDeleteTrans = strTrans
    
    Exit Function
EH:
    MsgBox Err.Description, vbCritical, g_strErrorLocation
End Function


'---------------------------------------------------------------------------------------
' Module    : ChangeTDStatus
' DateTime  : 10/28/2004
' Author    : Chris Elwell
' Purpose   : Changes a TD status to Confirmed or Deleted in the TD table.
' Call from : g_clsParser_ParseComplete()
'---------------------------------------------------------------------------------------
'
Private Sub ChangeTDStatus(ByVal strTrkNum As String, ByVal strStatus As String)
    Dim strQuery As String
    Dim Start As Long
    Dim rsTrkNum As New ADODB.Recordset
    
    On Error GoTo EH
    g_strErrorLocation = "frmShip:ChangeTDStatus"
    
    strQuery = "UPDATE TD SET [TDStatus] = " & SingleQuotes(strStatus) & " WHERE g_str1123 = " & SingleQuotes(strTrkNum)
    g_connFSMSClient.Execute strQuery
    If strStatus = "Deleted" Then
        strQuery = "UPDATE TD SET [Status] = " & SingleQuotes(strStatus) & " WHERE g_str1123 = " & SingleQuotes(strTrkNum)
        g_connFSMSClient.Execute strQuery
    End If
    
    'This one second delay allows update to complete before query is re-run.
    If g_blnDeletesComplete Then
        Start = Timer
        Do While Timer < Start + 1
            DoEvents
        Loop
    End If
    
    Exit Sub
EH:
     MsgBox Err.Description, vbCritical, g_strErrorLocation
End Sub


'---------------------------------------------------------------------------------------
' Module    : ChangeTDCStatus
' DateTime  : 10/28/2004
' Author    : Chris Elwell
' Purpose   : Changes a TDC status to Deleted in the TD table.
' Call from : g_clsParser_ParseComplete()
'---------------------------------------------------------------------------------------
'
Private Sub ChangeTDCStatus(ByVal strTrkNum As String)
    Dim rsQuery As String
    Dim Start As Long
    
    On Error GoTo EH
    g_strErrorLocation = "frmShip:ChangeTDCStatus"
    
    rsQuery = "UPDATE TD SET [Status] = 'Deleted' WHERE g_str29 = " & SingleQuotes(strTrkNum)

    g_connFSMSClient.Execute rsQuery
    
    'This one second delay allows update to complete before query is re-run.
    If g_blnDeletesComplete Then
        Start = Timer
        Do While Timer < Start + 1
            DoEvents
        Loop
    End If
    
    Exit Sub
EH:
     MsgBox Err.Description, vbCritical, g_strErrorLocation
End Sub


'---------------------------------------------------------------------------------------
' Module    : SaveTDShipment
' DateTime  : 3/17/2004
' Author    : Chris Elwell
' Purpose   : Store TD shipment fields to database
' Call from : g_clsParser_ParseComplete()
'---------------------------------------------------------------------------------------
'
Public Sub SaveTDShipment(ByVal strTrkNum As String, ByVal blnIsCreate As Boolean, Optional ByVal strMasterTrkNum As String)
    Dim strInsertQuery As String
    Dim rsTD As New ADODB.Recordset
    Dim strQuery As String
    
    On Error GoTo EH
    'g_strErrorLocation = "frmShip:SaveTDShipment"
        
    '        If Not blnIsCreate Then
    '            strQuery = "SELECT g_str1297 FROM TD WHERE TDStatus = 'Open' And g_str1355 = " & SingleQuotes(g_str1355)
    '            rsTD.Open strQuery, g_connFSMSClient, adOpenStatic, adLockOptimistic
    '
    '            g_str1297 = rsf1297!g_str1297
    '        End If
    
    strInsertQuery = "INSERT INTO TD ( OpenShipIndex, TDStatus, MAWB, " _
        & "g_str29, g_str1123, g_str25, g_str50, g_str10, g_str24, " _
        & "ReturnAddressID, IORID, f66, f67, f68, f70, f71, f72, f75, " _
        & "f113, f119, f1358, f1483, f1484, f1498, f1500, f1502, f1503, f1546, " _
        & "Status, ProcessTime ) "
    
    strInsertQuery = strInsertQuery + " VALUES ( "
    If g_str542 <> "" Then
        strInsertQuery = strInsertQuery + SingleQuotes(g_str542) + ","
    ElseIf g_strTDOpenShip <> "" Then
        strInsertQuery = strInsertQuery + SingleQuotes(g_strTDOpenShip) + ","
    End If
    strInsertQuery = strInsertQuery + SingleQuotes("Open") + ","
    'If it's a MAWB Create, set MAWB to 1
    strInsertQuery = strInsertQuery + "1,"
    strInsertQuery = strInsertQuery + SingleQuotes(strTrkNum) + ","
    strInsertQuery = strInsertQuery + SingleQuotes(strTrkNum) + ","
    strInsertQuery = strInsertQuery + SingleQuotes(FormTDAdd.f25) + ","
    strInsertQuery = strInsertQuery + SingleQuotes(FormTDAdd.tdReturn("f50")) + ","
    strInsertQuery = strInsertQuery + SingleQuotes(g_str10) + ","
    strInsertQuery = strInsertQuery + SingleQuotes(Format(g_dteTDDate, "YYYYMMDD")) + ","
    
    strInsertQuery = strInsertQuery + SingleQuotes(FormTDAdd.cboReturn) + ","
    strInsertQuery = strInsertQuery + SingleQuotes(FormTDAdd.cboIOR) + ","
    strInsertQuery = strInsertQuery + SingleQuotes(FormTDAdd.f66) + ","
    strInsertQuery = strInsertQuery + SingleQuotes(FormTDAdd.f67) + ","
    strInsertQuery = strInsertQuery + SingleQuotes(FormTDAdd.f68) + ","
    strInsertQuery = strInsertQuery + SingleQuotes(Left(FormTDAdd.f70, 1)) + ","
    strInsertQuery = strInsertQuery + SingleQuotes(FormTDAdd.f71) + ","
    strInsertQuery = strInsertQuery + SingleQuotes(Left(FormTDAdd.f72, 1)) + ","
    strInsertQuery = strInsertQuery + SingleQuotes(FormTDAdd.f75) + ","
    strInsertQuery = strInsertQuery + SingleQuotes(IIf(FormTDAdd.f113, "Y", "N")) + ","
    strInsertQuery = strInsertQuery + SingleQuotes(FormTDAdd.f119) + ","
    strInsertQuery = strInsertQuery + SingleQuotes(FormTDAdd.f1358) + ","
    strInsertQuery = strInsertQuery + SingleQuotes(FormTDAdd.f1483) + ","
    strInsertQuery = strInsertQuery + SingleQuotes(IIf(FormTDAdd.f1484, "Y", "N")) + ","
    strInsertQuery = strInsertQuery + SingleQuotes(Left(FormTDAdd.f1498, 1)) + ","
    strInsertQuery = strInsertQuery + SingleQuotes(FormTDAdd.f1500) + ","
    strInsertQuery = strInsertQuery + SingleQuotes(IIf(FormTDAdd.f1502Y, "Y", "N")) + ","
    strInsertQuery = strInsertQuery + SingleQuotes(FormTDAdd.f1503) + ","
    strInsertQuery = strInsertQuery + SingleQuotes(Left(FormTDAdd.f1546, 1)) + ","

    strInsertQuery = strInsertQuery + "'Shipped',"
    strInsertQuery = strInsertQuery + "#" + CStr(Now) + "#)"
    
    'Debug.Print Now, "SaveTDShipment"
    'Debug.Print strInsertQuery
    
    g_connFSMSClient.Execute strInsertQuery
    
    g_strTDOpenShip = g_str542
    g_strTDMasterTrkNbr = strTrkNum
    g_dteTDDate = FormTDAdd.f24
    
    g_clsReg.SetRegistryValue "TDMasterShipmentID", g_strTDMasterTrkNbr, REG_SZ
    g_clsReg.SetRegistryValue "TDMasterShipmentDate", Format(g_dteTDDate, "yyyy-mm-dd"), REG_SZ
    g_clsReg.SetRegistryValue "TDOpenShipIndex", g_strTDOpenShip, REG_SZ
    
    frmShip.DomIntlScreenLayout
    frmShip.f24 = g_dteTDDate
        
Release:
    If rsTD.State = adStateOpen Then rsTD.Close
    Set rsTD = Nothing
    
    Exit Sub
EH:
     MsgBox Err.Description, vbCritical, "frmShip:SaveTDShipment"

End Sub



'---------------------------------------------------------------------------------------
' Module    : SaveTDCShipment
' DateTime  : 3/17/2004
' Author    : Chris Elwell
' Purpose   : Store TD shipment fields to database
' Call from : g_clsParser_ParseComplete()
'---------------------------------------------------------------------------------------
'
Public Sub SaveTDCShipment(ByVal strTrkNum As String, ByVal blnIsCreate As Boolean, Optional ByVal strMasterTrkNum As String)
    Dim strInsertQuery As String
    Dim rsTD As New ADODB.Recordset
    Dim strQuery As String
    
    On Error GoTo EH
    g_strErrorLocation = "frmShip:SaveTDCShipment"
    
    ' 2006-12-18, JFW, Child shipment IRS number
    strInsertQuery = "INSERT INTO TD ( OpenShipIndex, TDStatus, MAWB, " _
        & "g_str29, g_str1123, g_str25, g_str50, g_str10, g_str24, " _
        & "g_str3056, g_str3057, g_str11, g_str12, g_str13, g_str15, g_str16, g_str17, " _
        & "g_str1274, g_str116, f1358, " _
        & "Status, ProcessTime ) "
    strInsertQuery = strInsertQuery + " VALUES ( "
    If g_str542 <> "" Then
        strInsertQuery = strInsertQuery + SingleQuotes(g_str542) + ","
    ElseIf g_strTDOpenShip <> "" Then
        strInsertQuery = strInsertQuery + SingleQuotes(g_strTDOpenShip) + ","
    End If
    strInsertQuery = strInsertQuery + SingleQuotes("Open") + ","
    'If it's a MAWB Create, set MAWB to 1
    strInsertQuery = strInsertQuery + "0,"
    strInsertQuery = strInsertQuery + SingleQuotes(strTrkNum) + ","
    strInsertQuery = strInsertQuery + SingleQuotes(g_strTDMasterTrkNbr) + ","
    strInsertQuery = strInsertQuery + SingleQuotes(g_str25) + ","
    If g_str50 <> "" Then
        strInsertQuery = strInsertQuery + SingleQuotes(g_str50) + ","
    Else
        strInsertQuery = strInsertQuery + SingleQuotes("US") + ","
    End If
    strInsertQuery = strInsertQuery + SingleQuotes(g_str10) + ","
    strInsertQuery = strInsertQuery + SingleQuotes(Format(g_dteTDDate, "YYYYMMDD")) + ","
    
    strInsertQuery = strInsertQuery + SingleQuotes(g_str3056) + ","
    strInsertQuery = strInsertQuery + SingleQuotes(g_str3057) + ","
    strInsertQuery = strInsertQuery + SingleQuotes(g_str11) + ","
    strInsertQuery = strInsertQuery + SingleQuotes(g_str12) + ","
    strInsertQuery = strInsertQuery + SingleQuotes(g_str13) + ","
    strInsertQuery = strInsertQuery + SingleQuotes(g_str15) + ","
    strInsertQuery = strInsertQuery + SingleQuotes(g_str16) + ","
    strInsertQuery = strInsertQuery + SingleQuotes(g_str17) + ","
    strInsertQuery = strInsertQuery + SingleQuotes(g_str1274) + ","
    strInsertQuery = strInsertQuery + SingleQuotes(g_str116) + ","
    
    ' 2006-12-18, JFW, Child shipment IRS number
    strInsertQuery = strInsertQuery + SingleQuotes(g_str118) + ","
    
    strInsertQuery = strInsertQuery + "'Shipped',"
    strInsertQuery = strInsertQuery + "#" + CStr(Now) + "#)"
    
    g_connFSMSClient.Execute strInsertQuery
    
Release:
    If rsTD.State = adStateOpen Then rsTD.Close
    Set rsTD = Nothing
    
    Exit Sub
EH:
     MsgBox Err.Description, vbCritical, g_strErrorLocation

End Sub


Public Sub ShipTD()

    Dim i As Integer
    Dim ret As VbMsgBoxResult
    Dim strReference As String
    Dim strTrans As String
    Dim colTags As Collection
    Dim strMasterTrackingNbr As String
    Dim intPieceCount As Long
    
    On Error GoTo EH
    
    g_strErrorLocation = "ShipTD"

    If g_strTDOpenShip = "" Then
        Beep
        frmShip.StatusBar1.Panels(2) = "Choose a TD master..."
        Set FormTD = New frmTD
        FormTD.Show vbModeless, frmShip
        Exit Sub
    End If
    
    If g_strTDOpenShip = "" Then
        Beep
        frmShip.StatusBar1.Panels(2) = "Choose a TD master..."
        Exit Sub
    End If
    
    g_strErrorLocation = "modCustomButtons:CustomButton2_TDShip"

    'Show Processing in status bar
    frmShip.StatusBar1.Panels(2) = "Processing..."

    'Disable all transaction buttons
    Call frmShip.DisableButtons

    intPieceCount = Val(frmShip.f116)
    If Not g_rsMPS Is Nothing Then
        If g_rsMPS.RecordCount > 0 Then
            intPieceCount = g_rsMPS.RecordCount
        End If
    End If
    
    If intPieceCount > 1 Then
        frmShip.f3057 = Replace(frmShip.f3057, ", Pkg <n> of <m>", "")
        frmShip.f3057 = Replace(frmShip.f3057, "Pkg <n> of <m>", "")
    End If

    'Assign the text fields to global vars
    Call frmShip.AssignTextToGlobal

    If intPieceCount > 1 Then
        strReference = frmShip.f3057 & IIf(frmShip.f3057 <> "", ", ", "") & "Pkg <n> of <m>"
    End If
    
    Set colTags = New Collection
    colTags.Add g_str24, "g_str24"
    colTags.Add g_str11, "g_str11"
    colTags.Add g_str12, "g_str12"
    colTags.Add g_str13, "g_str13"
    colTags.Add g_str15, "g_str15"
    colTags.Add g_str16, "g_str16"
    colTags.Add g_str17, "g_str17"
    colTags.Add g_str50, "g_str50"
    colTags.Add g_str25, "g_str25"
    colTags.Add g_str1274, "g_str1274"
    colTags.Add g_str116, "g_str116"
    colTags.Add g_str1670, "g_str1670"
    colTags.Add g_str3056, "g_str3056"
    colTags.Add g_str1358, "g_str1358"
    colTags.Add g_str118, "g_str118"

    g_intErrorCount = 0
    g_intShipCount = 0
    
    strTrans = BuildTDShipTrans(g_str1, 1, strMasterTrackingNbr)

    For g_intShipCount = 1 To intPieceCount
        
        ' 2006-10-05, JFW, Build package with shipment weight
        If intPieceCount > 1 Then
            TransactionStripTag strTrans, "116"
            TransactionStripTag strTrans, "1670"
            TransactionBuilder strTrans, "116", "1"
            If frmShip.chkDuplicate.Value = 1 Then
                TransactionBuilder strTrans, "1670", CLng(CDbl(g_str1670) * 100)
            Else
                TransactionBuilder strTrans, "1670", CLng(CDbl(g_str1670) * 100 / CDbl(g_str116))
            End If
            
            g_str3057 = strReference
            g_str3057 = Replace(g_str3057, "<n>", g_intShipCount)
            g_str3057 = Replace(g_str3057, "<m>", intPieceCount)
            TransactionStripTag strTrans, "3002"
            TransactionStripTag strTrans, "3057"
            TransactionBuilder strTrans, "3002", g_str3057
            TransactionBuilder strTrans, "3057", g_str3057
            
            strTrans = Replace(strTrans, "99,""""", "")
            TransactionBuilder strTrans, 99, ""
        End If
        
        ' If MPS is used, then change the weight
        If Not g_rsMPS Is Nothing Then
            If g_rsMPS.RecordCount > 0 Then
                g_rsMPS.AbsolutePosition = g_intShipCount
                TransactionStripTag strTrans, "21"
                TransactionStripTag strTrans, "1670"
                If IsNumeric(g_rsMPS!g_str1670) Then
                    TransactionBuilder strTrans, "1670", CLng(CLng(CDbl(g_rsMPS!g_str1670) + 0.49999) * 100)
                    g_str1670 = CLng(CDbl(g_rsMPS!g_str1670) + 0.49999)
                Else
                    TransactionBuilder strTrans, "1670", "100"
                    g_str1670 = 1
                End If
                strTrans = Replace(strTrans, "99,""""", "")
                TransactionBuilder strTrans, 99, ""
                'f1  g_str1670   g_str57 g_str58 g_str59 g_str26 g_str25 g_str3056   g_str3057   g_str3018   ShipDate
            End If
        End If
        
        If g_intShipCount = 2 Then
            strMasterTrackingNbr = g_str29
        End If
        If g_intShipCount > 1 Then
            TransactionStripTag strTrans, "77"
            TransactionStripTag strTrans, "79"
            TransactionBuilder strTrans, "79", "Package " & g_intShipCount _
                    & " of " & intPieceCount _
                    & ", master tracking number " & strMasterTrackingNbr
            TransactionStripTag strTrans, "80"
            TransactionBuilder strTrans, "80", "CA"
            TransactionStripTag strTrans, "81"
            TransactionStripTag strTrans, "82"
            TransactionBuilder strTrans, "82", "1"
            TransactionStripTag strTrans, "83"
            TransactionStripTag strTrans, "84"
            TransactionStripTag strTrans, "414"
            TransactionBuilder strTrans, "414", "EA"
            TransactionStripTag strTrans, "1030"
            TransactionBuilder strTrans, "1030", "00000010000"
            TransactionStripTag strTrans, "119"
            TransactionBuilder strTrans, "119", "001"
            
            ' Remove email from additional pieces
            TransactionStripTag strTrans, "1553"
            TransactionStripTag strTrans, "1554"
            TransactionStripTag strTrans, "1556"
            TransactionStripTag strTrans, "1557"
            TransactionStripTag strTrans, "1206"
            TransactionStripTag strTrans, "1551"
            
            strTrans = Replace(strTrans, "99,""""", "")
            TransactionBuilder strTrans, 99, ""
        End If
        
        If g_blnAbortBatch Then
            ret = MsgBox("Click OK to resume processing, click Cancel to abort processing.", vbOKCancel, "Batch Processing Paused")
    
            If ret = vbOK Then
                g_blnAbortBatch = False
            Else
                Exit For
            End If
        End If
         
        'CIC defined routine that runs before shipping the package.
        'Return True to continue shipment, False to Cancel.
        If BeforeShip(strTrans) Then
            
            ' 2007-05-15, JFW, New TD child path
            'g_blnTD = True
            g_blnTDChild = True
            Debug.Print Now, "ShipTD", g_blnTDChild
            g_blnComplete = False
            
            Call SendData(strTrans)
            
            Do While Not g_blnComplete
                DoEvents
            Loop
        
            If g_str3 <> "" Then
                Exit For
            End If
        End If
        
        g_str24 = colTags.item("g_str24")
        g_str11 = colTags.item("g_str11")
        g_str12 = colTags.item("g_str12")
        g_str13 = colTags.item("g_str13")
        g_str15 = colTags.item("g_str15")
        g_str16 = colTags.item("g_str16")
        g_str17 = colTags.item("g_str17")
        g_str50 = colTags.item("g_str50")
        g_str25 = colTags.item("g_str25")
        g_str1274 = colTags.item("g_str1274")
        g_str116 = colTags.item("g_str116")
        g_str1670 = colTags.item("g_str1670")
        g_str3056 = colTags.item("g_str3056")
        g_str1358 = colTags.item("g_str1358")
        g_str118 = colTags.item("g_str118")
            
    Next g_intShipCount

    frmShip.SSTab1.Tab = 0

    ClearShipScreen

    Exit Sub
EH:
     MsgBox Err.Description, vbCritical, g_strErrorLocation

End Sub


'---------------------------------------------------------------------------------------
' Module    : TDReply()
' DateTime  : 2/10/2005
' Author    : Chris Elwell
' Purpose   : Handle TD Replies. Moved out of ParseComplete.
'---------------------------------------------------------------------------------------
'
Public Sub TDReply()
    
    On Error GoTo EH
    'g_strErrorLocation = "frmShip:TDReply"
    
    g_blnTD = False
    
    If g_str3 = "" Then
        Select Case Left(g_str1, 15)
            'Create shipment
            Case "TD MAWB CREATE"
                Call SaveTDShipment(g_str29, True)
                Call FormTD.UpdateTDGrid("Open", FormTDAdd.f24)
                frmShip.StatusBar1.Panels(2) = "TD MAWB Created"
                g_strTDMasterTrkNbr = g_str29
                
                g_clsReg.SetRegistryValue "TDMasterShipmentID", g_strTDMasterTrkNbr, REG_SZ
                g_clsReg.SetRegistryValue "TDMasterShipmentDate", Format(g_dteTDDate, "yyyy-mm-dd"), REG_SZ
                g_clsReg.SetRegistryValue "TDOpenShipIndex", g_strTDOpenShip, REG_SZ
                            
                On Error Resume Next
                Unload FormTDAdd
                On Error GoTo EH
                
            'Delete shipment
            Case "TD MAWB DELETE"
                Call ChangeTDStatus(g_strTDMasterTrkNbr, "Deleted")
                Call FormTD.UpdateTDGrid("Deleted", FormTD.f24)
                frmShip.StatusBar1.Panels(2) = "TD MAWB Deleted"
                g_strTDMasterTrkNbr = ""
            
                g_clsReg.SetRegistryValue "TDMasterShipmentID", g_strTDMasterTrkNbr, REG_SZ
                g_clsReg.SetRegistryValue "TDMasterShipmentDate", Format(g_dteTDDate, "yyyy-mm-dd"), REG_SZ
                g_clsReg.SetRegistryValue "TDOpenShipIndex", g_strTDOpenShip, REG_SZ
                        
            'Confirm shipment
            Case "TD MAWB CONFRM"
                Call ChangeTDStatus(g_str1123, "Confirmed")
                Call FormTD.UpdateTDGrid("Confirmed", FormTD.f24)
                frmShip.StatusBar1.Panels(2) = "TD MAWB Confirmed"
                g_strTDMasterTrkNbr = ""
                On Error Resume Next
                Unload FormTDAdd
                On Error GoTo EH
            
                g_clsReg.SetRegistryValue "TDMasterShipmentID", g_strTDMasterTrkNbr, REG_SZ
                g_clsReg.SetRegistryValue "TDMasterShipmentDate", Format(g_dteTDDate, "yyyy-mm-dd"), REG_SZ
                g_clsReg.SetRegistryValue "TDOpenShipIndex", g_strTDOpenShip, REG_SZ
            
            'Delete shipment
            Case "TD TDC DELETE"
                Call ChangeTDCStatus(g_str29)
                Call WriteDelete(g_str29)
                frmShip.StatusBar1.Panels(2) = "TD Shipment Deleted"
                frmShip.StatusBar1.Panels(2) = g_str29 & " Deleted!"
                If g_blnDeletesComplete Then
                    Call frmShip.UpdateShipmentsGrid
                    Call frmShip.SetTrackButtons
                End If
            
                g_clsReg.SetRegistryValue "TDMasterShipmentID", g_strTDMasterTrkNbr, REG_SZ
                g_clsReg.SetRegistryValue "TDMasterShipmentDate", Format(g_dteTDDate, "yyyy-mm-dd"), REG_SZ
                g_clsReg.SetRegistryValue "TDOpenShipIndex", g_strTDOpenShip, REG_SZ
            
            ' 2007-05-15, JFW, No longer used for TD child, see SaveShipment
            'Add TDC
            Case Else
                ' 2006-11-07, JFW, Required here.
                If frmShip.cboCustID.Visible = True And frmShip.chkAddAddress = 1 Then
                    Call frmShip.SaveAddress(frmShip.cboCustID)
                    Call frmShip.LoadAddrLookupCombos(frmShip.cboCustID, frmShip.cboCustID, "CustID")
                    Call frmShip.LoadAddrLookupCombos(frmShip.cboContact, frmShip.cboContact, "f12")
                    Call frmShip.LoadAddrLookupCombos(frmShip.cboCompany, frmShip.cboContact, "f11")
                End If
                                    
                Call SaveTDCShipment(g_str29, False, g_str1123)
                Call WriteReply
                frmShip.StatusBar1.Panels(2) = "Shipment Complete"
        End Select
        
        Call frmShip.ClearShipFields
        Call ClearGlobalVars
        Call frmShip.SetLogo
    
    Else
        Call WriteError
        frmShip.StatusBar1.Panels(2) = "Shipment Error"
    End If
    'Make sure the TD flag is set to the correct boolean value
    
    Exit Sub
EH:
    MsgBox Err.Description, vbCritical, "frmShip:TDReply"

End Sub

